﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 *  MVC控制器基类。
 * 
 * 最后修改：2011-11-1
 * ************************************************************/
namespace Mozlite.Web.UI.Areas.ControlPanel.Controllers
{
    using System;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;
    using Mozlite.Common;

    /// <summary>
    /// 站点控制器。
    /// </summary>
    [SAuthorize("AllowHostManagement")]
    public class HostController : ControlPanelBaseController
    {
        public ActionResult Index()
        {
            return View(SiteDomains.LoadSites().Values);
        }

        [HttpPost]
        public JsonResult Index(FormCollection form)
        {
            switch (form.GetAction())
            {
                case HttpAction.Delete:
                    {
                        var site = SiteDomains.GetSite(form.Get<int>("id"));
                        if (site != null)
                        { 
                            if(site.IsDefault)
                                return Json(new { message = Local("Sites.Delete.DefaultError") });
                            if (SiteDomains.DeleteSite(site.SiteID))
                                return Json(new { message = Local("Sites.Delete.Success"), status = true });
                            return Json(new { message = Local("Sites.Delete.Error") });
                        }
                    }
                    break;
            }
            return Json(null);
        }

        public ActionResult AppView()
        {
            return View(Applications.LoadApplications().Values);
        }

        [HttpPost]
        public ActionResult AppView(FormCollection form)
        {
            foreach(var type in Globals.FindType<ApplicationBase>())
            {
                type.Save();
            }
            return RedirectToAction("AppView");
        }

        public ActionResult SettingView()
        {
            return View(SiteSettings.Current);
        }

        [HttpPost]
        public ActionResult SettingView(SiteSettings model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                bool bReset = model.PageMode != Settings.PageMode;
                model.SettingsID = Settings.SettingsID;
                model.Save();
                if (bReset)
                {
                    Globals.ResetApplication();
                    return Redirect(CUrls.DefaultUrl);
                }
                SetResourceStatus("SiteSettings.SaveSuccess");
            }
            return View(model);
        }

        public ActionResult UrlView()
        {
            string areaName = Request.QueryString["area"] ?? Routes.NullArea;
            Dictionary<string, Route> routes;
            Routes.LoadRoutes().TryGetValue(areaName, out routes);
            ViewBag.Area = areaName;
            return View(routes);
        }

        public ActionResult ControlPanelView()
        {
            return View(SecurityApplication.Current.Configuration);
        }

        [HttpPost]
        public ActionResult ControlPanelView(SecurityConfiguration model)
        {
            if (ModelState.IsValid)
            {
                model.ApplicationID = SecurityApplication.Current.ApplicationID;
                if (Applications.SaveConfiguration<SecurityConfiguration>(model))
                    SetResourceStatus("ConfigurationBase.Save.Success");
                else
                    SetResourceStatus("ConfigurationBase.Save.Error", true);
            }
            return View(SecurityApplication.Current.Configuration);
        }

        public ActionResult TaskView()
        {
            return View(Tasks.Threads);
        }

        public ActionResult PermissionView()
        {
            return View(Permissions.LoadPermissions<SecurityPermission>());
        }

        [HttpPost]
        public ActionResult PermissionView(FormCollection form)
        {
            var permissions = new List<SecurityPermission>();
            foreach (var role in Roles.GetRoles(RoleLevel.Administrator))
            {
                permissions.Add(SecurityPermission.GetForm<SecurityPermission>(role.RoleID, form));
            }
            if (Permissions.SavePermissions<SecurityPermission>(permissions))
                SetResourceStatus("Permission.Save.Success");
            else
                SetResourceStatus("Permission.Save.Error");
            return View(Permissions.LoadPermissions<SecurityPermission>());
        }

        public ActionResult LogView()
        {
            int size;
            var query = new LogQuery { PageSize = AppConfig.LogPageSize, PageIndex = PageIndex - 1, IsDesc = true, OrderBy = "LogID" };
            var logs = Logs.LoadLogs(query, out size);
            ViewBag.Pages = GetPages(size, query.PageSize);
            return View(logs);
        }
    }
}
